home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Drivers / ghostHPDJ / gdevcdj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-23  |  49.9 KB  |  1,609 lines

  1. /* Copyright (C) 1991, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* THIS IS A MODIFIED VERSION OF THE gdevcdj.c DISTRIBUTED WITH */
  20. /* GHOSTSCRIPT 2.61. MODIFICATION FOR COLOR CORRECTION ADDED    */
  21. /* BY STEVE LUDTKE 8/15/94.                                     */
  22.  
  23. /* gdevcdj.c */
  24. /* H-P colour printer drivers for Ghostscript */
  25. #include "std.h"                /* to stop stdlib.h redefining types */
  26. #include <stdlib.h>        /* for rand() */
  27. #include "gdevprn.h"
  28. #include "gdevpcl.h"
  29. #include "gsprops.h"
  30.  
  31. /***
  32.  *** Note: this driver was contributed by a user, George Cameron:
  33.  ***       please contact g.cameron@biomed.abdn.ac.uk if you have questions.
  34.  ***/
  35.  
  36. /*
  37.  * Note that there are six drivers contained in this code:
  38.  *
  39.  *     1 - cdj500:      HP DeskJet 500C
  40.  *     2 - cdj550:      HP DeskJet 550C
  41.  *     3 - pjxl300:     HP PaintJet XL300
  42.  *     4 - pj:          HP PaintJet
  43.  *     5 - pjxl:        HP PaintJet XL
  44.  *     6 - declj250:    DEC LJ250
  45.  *
  46.  * All of these drivers have 8-bit (monochrome), 16-bit and 24-bit
  47.  *     (colour) and for the DJ 550C 32-bit, (colour, cmyk mode)
  48.  *     options in addition to the usual 1-bit and 3-bit modes
  49.  * It is also possible to set various printer-specific parameters
  50.  *     from the gs command line, eg.
  51.  *
  52.  *  gs -sDEVICE=cdj550 -dBitsPerPixel=16 -dDepletion=1 -dShingling=2 tiger.ps
  53.  *
  54.  * Please consult the appropriate section in the devices.doc file for
  55.  * further details on all these drivers.
  56.  */
  57.  
  58. #define DESKJET_PRINT_LIMIT  0.04       /* 'real' top margin? */
  59. #define PAINTJET_PRINT_LIMIT 0.0        /* This is a guess.. */
  60.  
  61. /* Margins are left, bottom, right, top. */
  62. #define DESKJET_MARGINS_LETTER   0.25, 0.50, 0.25, 0.167
  63. #define DESKJET_MARGINS_A4       0.125, 0.50, 0.143, 0.167
  64. #define PAINTJET_MARGINS_LETTER  0.167, 0.167, 0.167, 0.167
  65. #define PAINTJET_MARGINS_A4      0.167, 0.167, 0.167, 0.167
  66. #define PAINTJET_MARGINS_A3      0.167, 0.167, 0.167, 0.167
  67.  
  68. /* the next 2 lines are for color correction */
  69. float CCOR[9][9][9][3];    /* color correction matrix */
  70. char *TMPCCOR = "/usr/local/lib/ghostscript/filt/hpccor";
  71.  
  72. /* Default page size is US-Letter or A4 (other sizes from command line) */
  73. #ifndef A4
  74. #  define WIDTH_10THS            85
  75. #  define HEIGHT_10THS           110
  76. #else
  77. #  define WIDTH_10THS            83      /* 210mm */
  78. #  define HEIGHT_10THS           117     /* 297mm */
  79. #endif
  80.  
  81. /* Define bits-per-pixel for generic drivers - default is 24-bit mode */
  82. #ifndef BITSPERPIXEL
  83. #  define BITSPERPIXEL 24
  84. #endif
  85.  
  86. #define W sizeof(word)
  87. #define I sizeof(int)
  88.  
  89. /* Printer types */
  90. #define DJ500C   0
  91. #define DJ550C   1
  92. #define PJXL300  2
  93. #define PJ180    3
  94. #define PJXL180  4
  95. #define DECLJ250 5
  96.  
  97. /* No. of ink jets (used to minimise head movements) */
  98. #define HEAD_ROWS_MONO 50
  99. #define HEAD_ROWS_COLOUR 16
  100.  
  101. /* Colour mapping procedures */
  102. private dev_proc_map_rgb_color (gdev_pcl_map_rgb_color);
  103. private dev_proc_map_color_rgb (gdev_pcl_map_color_rgb);
  104.  
  105. /* Print-page, properties and miscellaneous procedures */
  106. private dev_proc_open_device(dj500c_open);
  107. private dev_proc_open_device(dj550c_open);
  108. private dev_proc_open_device(pjxl300_open);
  109. private dev_proc_open_device(pjxl_open);
  110. private dev_proc_open_device(pj_open);
  111. private dev_proc_print_page(dj500c_print_page);
  112. private dev_proc_print_page(dj550c_print_page);
  113. private dev_proc_print_page(pjxl_print_page);
  114. private dev_proc_print_page(pj_print_page);
  115. private dev_proc_print_page(pjxl300_print_page);
  116. private dev_proc_print_page(declj250_print_page);
  117. private dev_proc_get_props(cdj_get_props);
  118. private dev_proc_get_props(pjxl_get_props);
  119. private dev_proc_get_props(pj_get_props);
  120. private dev_proc_put_props(cdj_put_props);
  121. private dev_proc_put_props(pjxl_put_props);
  122. private dev_proc_put_props(pj_put_props);
  123.  
  124. /* The device descriptors */
  125. typedef struct gx_device_cdj_s gx_device_cdj;
  126. struct gx_device_cdj_s {
  127.         gx_device_common;
  128.         gx_prn_device_common;
  129.     int correction;           /* Black correction parameter */
  130.     int shingling;          /* Interlaced, multi-pass printing */
  131.     int depletion;          /* 'Intelligent' dot-removal */
  132. };
  133.  
  134. typedef struct gx_device_pjxl_s gx_device_pjxl;
  135. struct gx_device_pjxl_s {
  136.         gx_device_common;
  137.         gx_prn_device_common;
  138.     uint correction;          /* Black correction parameter */
  139.     int printqual;            /* Mechanical print quality */
  140.     int rendertype;           /* Driver or printer dithering control */
  141. };
  142.  
  143. typedef struct gx_device_hp_s gx_device_hp;
  144. struct gx_device_hp_s {
  145.         gx_device_common;
  146.         gx_prn_device_common;
  147.     uint correction;          /* Black correction parameter
  148.                    * (used only by DJ500C) */
  149. };
  150.  
  151. typedef struct gx_device_hp_s gx_device_pj;
  152.  
  153. #define hp_device ((gx_device_hp *)pdev)
  154. #define cdj       ((gx_device_cdj *)pdev)
  155. #define pjxl      ((gx_device_pjxl *)pdev)
  156. #define pj    ((gx_device_pj *)pdev)
  157.  
  158. #define prn_hp_colour_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page)\
  159.     prn_device_body(gx_device_printer, procs, dev_name,\
  160.     WIDTH_10THS, HEIGHT_10THS, x_dpi, y_dpi, 0, 0, 0, 0, 0,\
  161.     bpp, 0, 0, 0, 0, print_page)
  162.  
  163. #define cdj_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page, correction, shingling, depletion)\
  164. { prn_hp_colour_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page),\
  165.     correction,\
  166.     shingling,\
  167.     depletion\
  168. }
  169.  
  170. #define pjxl_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page, printqual, rendertype)\
  171. { prn_hp_colour_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page), 0, \
  172.     printqual,\
  173.     rendertype\
  174. }
  175.  
  176. #define pj_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page)\
  177. { prn_hp_colour_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page), 0\
  178. }
  179.  
  180. #define hp_colour_procs(proc_colour_open, proc_get_props, proc_put_props) {\
  181.     proc_colour_open,\
  182.     gdev_pcl_get_initial_matrix,\
  183.     gx_default_sync_output,\
  184.     gdev_prn_output_page,\
  185.     gdev_prn_close,\
  186.     gdev_pcl_map_rgb_color,\
  187.     gdev_pcl_map_color_rgb,\
  188.     NULL,    /* fill_rectangle */\
  189.     NULL,    /* tile_rectangle */\
  190.     NULL,    /* copy_mono */\
  191.     NULL,    /* copy_color */\
  192.     NULL,    /* draw_line */\
  193.     gx_default_get_bits,\
  194.     proc_get_props,\
  195.     proc_put_props\
  196. }
  197.  
  198. private gx_device_procs cdj500_procs =
  199. hp_colour_procs(dj500c_open, cdj_get_props, cdj_put_props);
  200.  
  201. private gx_device_procs cdj550_procs =
  202. hp_colour_procs(dj550c_open, cdj_get_props, cdj_put_props);
  203.  
  204. private gx_device_procs pjxl300_procs =
  205. hp_colour_procs(pjxl300_open, pjxl_get_props, pjxl_put_props);
  206.  
  207. private gx_device_procs pjxl_procs =
  208. hp_colour_procs(pjxl_open, pjxl_get_props, pjxl_put_props);
  209.  
  210. private gx_device_procs pj_procs =
  211. hp_colour_procs(pj_open, pj_get_props, pj_put_props);
  212.  
  213. gx_device_cdj far_data gs_cdjmono_device =
  214. cdj_device(cdj500_procs, "cdjmono", 300, 300, 1,
  215.        dj500c_print_page, 4, 0, 1);
  216.  
  217. gx_device_cdj far_data gs_cdeskjet_device =
  218. cdj_device(cdj500_procs, "cdeskjet", 300, 300, 3,
  219.        dj500c_print_page, 4, 2, 1);
  220.  
  221. gx_device_cdj far_data gs_cdjcolor_device =
  222. cdj_device(cdj500_procs, "cdjcolor", 300, 300, 24,
  223.        dj500c_print_page, 4, 2, 1);
  224.  
  225. gx_device_cdj far_data gs_cdj500_device =
  226. cdj_device(cdj500_procs, "cdj500", 300, 300, BITSPERPIXEL,
  227.        dj500c_print_page, 4, 2, 1);
  228.  
  229. gx_device_cdj far_data gs_cdj550_device =
  230. cdj_device(cdj550_procs, "cdj550", 300, 300, BITSPERPIXEL,
  231.        dj550c_print_page, 0, 2, 1);
  232.  
  233. gx_device_pjxl far_data gs_pjxl300_device =
  234. pjxl_device(pjxl300_procs, "pjxl300", 300, 300, BITSPERPIXEL,
  235.        pjxl300_print_page, 0, 0);
  236.  
  237. gx_device_pjxl far_data gs_pjxl_device =
  238. pjxl_device(pjxl_procs, "pjxl", 180, 180, BITSPERPIXEL,
  239.        pjxl_print_page, 0, 0);
  240.  
  241. gx_device_pj far_data gs_pj_device =
  242. pj_device(pj_procs, "pj", 180, 180, BITSPERPIXEL,
  243.        pj_print_page);
  244.  
  245. gx_device_pj far_data gs_declj250_device =
  246. pj_device(pj_procs, "declj250", 180, 180, BITSPERPIXEL,
  247.        declj250_print_page);
  248.  
  249. /* Forward references */
  250. private int gdev_pcl_mode1compress(P3(const byte *, const byte *, byte *));
  251. private int gdev_pcl_mode9compress(P4(int, const byte *, const byte *, byte *));
  252. private int hp_colour_open(P2(gx_device *, int));
  253. private int hp_colour_print_page(P3(gx_device_printer *, FILE *, int));
  254. private int put_prop_int(P5(gs_prop_item *, int *, int, int, int));
  255. private uint gdev_prn_rasterwidth(P2(const gx_device_printer *, int));
  256. private void set_bpp(P2(gx_device *, int));
  257. private void expand_line(P4(word *, int, int, int));
  258.  
  259. /* Open the printer and set up the margins. */
  260. private int
  261. dj500c_open(gx_device *pdev)
  262. { FILE *in;
  263.  
  264. in=fopen(TMPCCOR,"r");
  265. if (in!=NULL) {
  266.     fread(CCOR,sizeof(float)*3,729,in);
  267.     fclose(in);
  268. }
  269. return hp_colour_open(pdev, DJ500C);
  270. }
  271.  
  272. private int
  273. dj550c_open(gx_device *pdev)
  274. { FILE *in;
  275.  
  276. in=fopen(TMPCCOR,"r");
  277. if (in!=NULL) {
  278.     fread(CCOR,sizeof(float)*3,729,in);
  279.     fclose(in);
  280. }
  281. return hp_colour_open(pdev, DJ550C);
  282. }
  283.  
  284. private int
  285. pjxl300_open(gx_device *pdev)
  286. { return hp_colour_open(pdev, PJXL300);
  287. }
  288.  
  289. private int
  290. pj_open(gx_device *pdev)
  291. { return hp_colour_open(pdev, PJ180);
  292. }
  293.  
  294. private int
  295. pjxl_open(gx_device *pdev)
  296. { return hp_colour_open(pdev, PJXL180);
  297. }
  298.  
  299. private int
  300. hp_colour_open(gx_device *pdev, int ptype)
  301. {       /* Change the margins if necessary. */
  302.   static const float pj_a4[4] = { PAINTJET_MARGINS_A4 };
  303.   static const float pj_a3[4] = { PAINTJET_MARGINS_A3 };
  304.   static const float pj_letter[4] = { PAINTJET_MARGINS_LETTER };
  305.   static const float dj_a4[4] = { DESKJET_MARGINS_A4 };
  306.   static const float dj_letter[4] = { DESKJET_MARGINS_LETTER };
  307.   const float _ds *m;
  308.   int psize;
  309.  
  310.   /* Set up colour params if put_props has not already done so */
  311.   if (pdev->color_info.num_components == 0)
  312.     set_bpp(pdev, pdev->color_info.depth);
  313.  
  314.   switch (ptype) {
  315.   case DJ500C:
  316.   case DJ550C:
  317.     m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? dj_a4 :
  318.      dj_letter);
  319.     break;
  320.   case PJ180:
  321.     m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? pj_a4 :
  322.      pj_letter);
  323.     break;
  324.   case PJXL300:
  325.   case PJXL180:
  326.     m = ((psize = gdev_pcl_paper_size(pdev)) == PAPER_SIZE_A4 ? pj_a4 :
  327.      (psize == PAPER_SIZE_A3) ? pj_a3 : pj_letter);
  328.     break;
  329.   }
  330.   pdev->l_margin = m[0];
  331.   pdev->b_margin = m[1];
  332.   pdev->r_margin = m[2];
  333.   pdev->t_margin = m[3];
  334.   return gdev_prn_open(pdev);
  335. }
  336.  
  337. /* Added properties for DeskJet 5xxC */
  338.  
  339. private const gs_prop_item props_cdj[] = {
  340.   /* Read-write properties. */
  341.   prop_def("BlackCorrect", prt_int),
  342.   prop_def("Shingling", prt_int),
  343.   prop_def("Depletion", prt_int),
  344.   prop_def("BitsPerPixel", prt_int),
  345. };
  346.  
  347. /* Get properties.  In addition to the standard and printer 
  348.  * properties, we supply shingling and depletion parameters,
  349.  * and control over the bits-per-pixel used in output rendering */
  350. private int
  351. cdj_get_props(gx_device *pdev, gs_prop_item *plist)
  352. {    int start = gdev_prn_get_props(pdev, plist);
  353.     if ( plist != 0 )
  354.        {    register gs_prop_item *pi = plist + start;
  355.         memcpy(pi, props_cdj, sizeof(props_cdj));
  356.         pi[0].value.i = cdj->correction;
  357.         pi[1].value.i = cdj->shingling;
  358.         pi[2].value.i = cdj->depletion;
  359.         pi[3].value.i = cdj->color_info.depth;
  360.        }
  361.     return start + sizeof(props_cdj) / sizeof(gs_prop_item);
  362. }
  363.  
  364. /* Put properties. */
  365. private int
  366. cdj_put_props(gx_device *pdev, gs_prop_item *plist, int count)
  367. {    gs_prop_item *known[4];
  368.     int old_bpp = cdj->color_info.depth;
  369.     int bpp = 0;
  370.     int code = 0;
  371.  
  372.     props_extract(plist, count, props_cdj, 4, known, 0);
  373.     code = gdev_prn_put_props(pdev, plist, count);
  374.     if ( code < 0 ) return code;
  375.  
  376.     code = put_prop_int(known[0], &cdj->correction, 0, 9, code);
  377.     code = put_prop_int(known[1], &cdj->shingling, 0, 2, code);
  378.     code = put_prop_int(known[2], &cdj->depletion, 1, 3, code);
  379.     code = put_prop_int(known[3], &bpp, 1, 32, code);
  380.  
  381.     if ( code < 0 )
  382.       return_error(code);
  383.  
  384.     if (bpp != 0) {
  385.       set_bpp(pdev, bpp);
  386.       
  387.       /* Close the device; gs_putdeviceprops will reopen it. */
  388.       if ( bpp != old_bpp && pdev->is_open )
  389.         { int ccode = gs_closedevice(pdev);
  390.           if ( ccode < 0 ) return ccode;
  391.         }
  392.     }
  393.     
  394.     return code;
  395. }
  396.  
  397. /* Added properties for PaintJet XL and PaintJet XL300 */
  398.  
  399. private const gs_prop_item props_pjxl[] = {
  400.   /* Read-write properties. */
  401.   prop_def("PrintQuality", prt_int),
  402.   prop_def("RenderType", prt_int),
  403.   prop_def("BitsPerPixel", prt_int),
  404. };
  405.  
  406. /* Get properties.  In addition to the standard and printer
  407.  * properties, we supply print_quality and render_type 
  408.  * parameters, together with bpp control. */
  409. private int
  410. pjxl_get_props(gx_device *pdev, gs_prop_item *plist)
  411. {    int start = gdev_prn_get_props(pdev, plist);
  412.     if ( plist != 0 )
  413.        {    register gs_prop_item *pi = plist + start;
  414.         memcpy(pi, props_pjxl, sizeof(props_pjxl));
  415.         pi[0].value.i = pjxl->printqual;
  416.         pi[1].value.i = pjxl->rendertype;
  417.         pi[2].value.i = pjxl->color_info.depth;
  418.        }
  419.     return start + sizeof(props_pjxl) / sizeof(gs_prop_item);
  420. }
  421.  
  422. /* Put properties. */
  423. private int
  424. pjxl_put_props(gx_device *pdev, gs_prop_item *plist, int count)
  425. {    gs_prop_item *known[3];
  426.     int old_bpp = pjxl->color_info.depth;
  427.     int bpp = 0;
  428.     int code = 0;
  429.  
  430.     props_extract(plist, count, props_pjxl, 3, known, 0);
  431.     code = gdev_prn_put_props(pdev, plist, count);
  432.     if ( code < 0 ) return code;
  433.  
  434.     code = put_prop_int(known[0], &pjxl->printqual, -1, 1, code);
  435.     code = put_prop_int(known[1], &pjxl->rendertype, 0, 10, code);
  436.     code = put_prop_int(known[2], &bpp, 1, 32, code);
  437.  
  438.     if ( code < 0 )
  439.       return_error(code);
  440.  
  441.     if (pjxl->rendertype > 0) {
  442.       /* If printer is doing the dithering, we must have a
  443.        * true-colour mode, ie. 16 or 24 bits per pixel */
  444.       if ((bpp == 0 && old_bpp < 16) || (bpp > 0 && bpp < 16))
  445.         bpp = 24;
  446.     }
  447.  
  448.     if (bpp != 0) {
  449.       set_bpp(pdev, bpp);
  450.       
  451.       /* Close the device; gs_putdeviceprops will reopen it. */
  452.       if ( bpp != old_bpp && pdev->is_open )
  453.         { int ccode = gs_closedevice(pdev);
  454.           if ( ccode < 0 ) return ccode;
  455.         }
  456.     }
  457.     
  458.     return code;
  459. }
  460.  
  461. /* Added properties for PaintJet */
  462.  
  463. private const gs_prop_item props_pj[] = {
  464.   /* Read-write properties. */
  465.   prop_def("BitsPerPixel", prt_int),
  466. };
  467.  
  468. /* Get properties.  In addition to the standard and printer */
  469. /* properties, we allow control of the bits-per-pixel */
  470. private int
  471. pj_get_props(gx_device *pdev, gs_prop_item *plist)
  472. {    int start = gdev_prn_get_props(pdev, plist);
  473.     if ( plist != 0 )
  474.        {    register gs_prop_item *pi = plist + start;
  475.         memcpy(pi, props_pj, sizeof(props_pj));
  476.         pi[0].value.i = pj->color_info.depth;
  477.        }
  478.     return start + sizeof(props_pj) / sizeof(gs_prop_item);
  479. }
  480.  
  481. /* Put properties. */
  482. private int
  483. pj_put_props(gx_device *pdev, gs_prop_item *plist, int count)
  484. {    gs_prop_item *known[1];
  485.     int old_bpp = pj->color_info.depth;
  486.     int bpp = 0;
  487.     int code = 0;
  488.  
  489.     props_extract(plist, count, props_pj, 1, known, 0);
  490.     code = gdev_prn_put_props(pdev, plist, count);
  491.     if ( code < 0 ) return code;
  492.  
  493.     code = put_prop_int(known[0], &bpp, 1, 32, code);
  494.  
  495.     if ( code < 0 )
  496.       return_error(code);
  497.  
  498.     if (bpp != 0) {
  499.       set_bpp(pdev, bpp);
  500.       
  501.       /* Close the device; gs_putdeviceprops will reopen it. */
  502.       if ( bpp != old_bpp && pdev->is_open )
  503.         { int ccode = gs_closedevice(pdev);
  504.           if ( ccode < 0 ) return ccode;
  505.         }
  506.     }
  507.     
  508.     return code;
  509. }
  510.  
  511. /* ------ Internal routines ------ */
  512.  
  513. /* The DeskJet500C can compress (mode 9) */
  514. private int
  515. dj500c_print_page(gx_device_printer * pdev, FILE * prn_stream)
  516. {
  517.   return hp_colour_print_page(pdev, prn_stream, DJ500C);
  518. }
  519.  
  520. /* The DeskJet550C can compress (mode 9) */
  521. private int
  522. dj550c_print_page(gx_device_printer * pdev, FILE * prn_stream)
  523. {
  524.   return hp_colour_print_page(pdev, prn_stream, DJ550C);
  525. }
  526.  
  527. /* The PJXL300 can compress (modes 2 & 3) */
  528. private int
  529. pjxl300_print_page(gx_device_printer * pdev, FILE * prn_stream)
  530. { int ret_code;
  531.   /* Ensure we're operating in PCL mode */
  532.   fputs("\033%-12345X@PJL enter language = PCL\n", prn_stream);
  533.   ret_code = hp_colour_print_page(pdev, prn_stream, PJXL300);
  534.   /* Reenter switch-configured language */
  535.   fputs("\033%-12345X", prn_stream);
  536.   return ret_code;
  537. }
  538.  
  539. /* The PaintJet XL can compress (modes 2 & 3) */
  540. private int
  541. pjxl_print_page(gx_device_printer * pdev, FILE * prn_stream)
  542. {
  543.   return hp_colour_print_page(pdev, prn_stream, PJXL180);
  544. }
  545.  
  546. /* The PaintJet can compress (mode 1) */
  547. private int
  548. pj_print_page(gx_device_printer * pdev, FILE * prn_stream)
  549. {
  550.   return hp_colour_print_page(pdev, prn_stream, PJ180);
  551. }
  552.  
  553. /* The LJ250 can compress (mode 1) */
  554. private int
  555. declj250_print_page(gx_device_printer * pdev, FILE * prn_stream)
  556. { int ret_code;
  557.   fputs("\033%8", prn_stream);    /* Enter PCL emulation mode */
  558.   ret_code = hp_colour_print_page(pdev, prn_stream, DECLJ250);
  559.   fputs("\033%@", prn_stream);    /* Exit PCL emulation mode */
  560.   return ret_code;
  561. }
  562.  
  563. /* MACROS FOR DITHERING (we use macros for compact source and faster code) */
  564. /* Floyd-Steinberg dithering. Often results in a dramatic improvement in
  565.  * subjective image quality, but can also produce dramatic increases in
  566.  * amount of printer data generated and actual printing time!! Mode 9 2D
  567.  * compression is still useful for fairly flat colour or blank areas but its
  568.  * compression is much less effective in areas where the dithering has
  569.  * effectively randomised the dot distribution. */
  570.  
  571. #define SHIFT (I * I)
  572. #define MINVALUE  0
  573. #define MAXVALUE  ((256 << SHIFT) - 1)
  574. #define THRESHOLD (128 << SHIFT)
  575.  
  576. #define FSditherI(inP, out, errP, Err, Bit, Offset)\
  577.     oldErr = Err;\
  578.     Err = (*errP + ((Err * 7) >> 4) + (*inP++ << SHIFT));\
  579.         if (Err > MAXVALUE) Err = MAXVALUE;\
  580.         else if (Err < MINVALUE) Err = MINVALUE;\
  581.     if (Err > THRESHOLD) {\
  582.       out |= Bit;\
  583.       Err -= MAXVALUE;\
  584.     }\
  585.     errP[Offset] += ((Err * 3) >> 4);\
  586.     *errP++ = ((Err * 5 + oldErr) >> 4);
  587.  
  588. #define FSditherD(inP, out, errP, Err, Bit, Offset)\
  589.     oldErr = Err;\
  590.     Err = (*--errP + ((Err * 7) >> 4) + (*--inP << SHIFT));\
  591.         if (Err > MAXVALUE) Err = MAXVALUE;\
  592.         else if (Err < MINVALUE) Err = MINVALUE;\
  593.     if (Err > THRESHOLD) {\
  594.       out |= Bit;\
  595.       Err -= MAXVALUE;\
  596.     }\
  597.     errP[Offset] += ((Err * 3) >> 4);\
  598.     *errP = ((Err * 5 + oldErr) >> 4);
  599.  
  600. /* Here we rely on compiler optimisation to remove lines of the form
  601.  * (if (1 >= 4) {...}, ie. the constant boolean expressions */
  602.  
  603. #define FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr, cP, mP, yP, kP, n)\
  604. {\
  605.     if (scan == 0) {       /* going_up */\
  606.       for (i = 0; i < plane_size; i++) {\
  607.     byte c, y, m, k, bitmask;\
  608.     int oldErr;\
  609.     bitmask = 0x80;\
  610.     for (c = m = y = k = j = 0; j < 8; j++) {\
  611.       if (n >= 4)\
  612.         { FSditherI(dp, k, ep, kErr, bitmask, -n);\
  613.         }\
  614.       if (n >= 3)\
  615.         { FSditherI(dp, c, ep, cErr, bitmask, -n);\
  616.           FSditherI(dp, m, ep, mErr, bitmask, -n);\
  617.         }\
  618.       FSditherI(dp, y, ep, yErr, bitmask, -n);\
  619.       bitmask >>= 1;\
  620.     }\
  621.     if (n >= 4)\
  622.       *kP++ = k;\
  623.     if (n >= 3)\
  624.       { *cP++ = c;\
  625.             *mP++ = m;\
  626.       }\
  627.         *yP++ = y;\
  628.       }\
  629.     } else {        /* going_down */\
  630.       for (i = 0; i < plane_size; i++) {\
  631.     byte c, y, m, k, bitmask;\
  632.     int oldErr;\
  633.     bitmask = 0x01;\
  634.     for (c = m = y = k = j = 0; j < 8; j++) {\
  635.       FSditherD(dp, y, ep, yErr, bitmask, n);\
  636.       if (n >= 3)\
  637.         { FSditherD(dp, m, ep, mErr, bitmask, n);\
  638.           FSditherD(dp, c, ep, cErr, bitmask, n);\
  639.         }\
  640.       if (n >= 4)\
  641.         { FSditherD(dp, k, ep, kErr, bitmask, n);\
  642.         }\
  643.       bitmask <<= 1;\
  644.     }\
  645.     *--yP = y;\
  646.     if (n >= 3)\
  647.       { *--mP = m;\
  648.         *--cP = c;\
  649.       }\
  650.     if (n >= 4)\
  651.       *--kP = k;\
  652.       }\
  653.     }\
  654. }
  655. /* END MACROS FOR DITHERING */
  656.  
  657. /* Some convenient shorthand .. */
  658. #define x_dpi        (pdev->x_pixels_per_inch)
  659. #define y_dpi        (pdev->y_pixels_per_inch)
  660. #define CONFIG_16BIT "\033*v6W\000\003\000\005\006\005"
  661. #define CONFIG_24BIT "\033*v6W\000\003\000\010\010\010"
  662.  
  663. /* To calculate buffer size as next greater multiple of both parameter and W */
  664. #define calc_buffsize(a, b) (((((a) + ((b) * W) - 1) / ((b) * W))) * W)
  665.  
  666. /* Send the page to the printer.  Compress each scan line. */
  667. private int
  668. hp_colour_print_page(gx_device_printer * pdev, FILE * prn_stream, int ptype)
  669. {
  670.   uint raster_width = gdev_prn_rasterwidth(pdev, 1);
  671. /*  int line_size = gdev_prn_rasterwidth(pdev, 0); */
  672.   int line_size = gdev_prn_raster(pdev);
  673.   int line_size_words = (line_size + W - 1) / W;
  674.   int paper_size = gdev_pcl_paper_size((gx_device *)pdev);
  675.   int num_comps = pdev->color_info.num_components;
  676.   int bits_per_pixel = pdev->color_info.depth;
  677.   int storage_bpp = bits_per_pixel;
  678.   int expanded_bpp = bits_per_pixel;
  679.   int plane_size, databuff_size;
  680.   int combined_escapes = 1;
  681.   int errbuff_size = 0;
  682.   int outbuff_size = 0;
  683.   int compression = 0;
  684.   int scan = 0;
  685.   int *errors[2];
  686.   char *cid_string;
  687.   byte *data[4], *plane_data[4][4], *out_data;
  688.   byte *out_row, *out_row_alt;
  689.   word *storage;
  690.   uint storage_size_words;
  691.  
  692.   /* Tricks and cheats ... */
  693.   switch (ptype) {
  694.   case DJ550C:
  695.     if (num_comps == 3)
  696.       num_comps = 4;                      /* 4-component printing */
  697.     break;
  698.   case PJXL300:
  699.   case PJXL180:
  700.     if (pjxl->rendertype > 0) {
  701.       if (bits_per_pixel < 16)
  702.     pjxl->rendertype = 0;
  703.       else {
  704.     /* Control codes for CID sequence */
  705.     cid_string = (bits_per_pixel == 16) ? CONFIG_16BIT : CONFIG_24BIT;
  706.     /* Pretend we're a monobit device so we send the data out unchanged */
  707.     bits_per_pixel = storage_bpp = expanded_bpp = 1;
  708.     num_comps = 1;
  709.       }
  710.     }
  711.     break;
  712.   }
  713.  
  714.   if (storage_bpp == 8 && num_comps >= 3)
  715.     bits_per_pixel = expanded_bpp = 3;  /* Only 3 bits of each byte used */
  716.  
  717.   plane_size = calc_buffsize(line_size, storage_bpp);
  718.  
  719.   if (bits_per_pixel == 1) {            /* Data printed direct from i/p */
  720.     databuff_size = 0;                  /* so no data buffer required, */
  721.     outbuff_size = plane_size * 4;      /* but need separate output buffers */
  722.   }
  723.   
  724.   if (bits_per_pixel > 4) {             /* Error buffer for FS dithering */
  725.     expanded_bpp = storage_bpp =        /* 8, 24 or 32 bits */
  726.       num_comps * 8;
  727.     errbuff_size =                      /* 4n extra values for line ends */
  728.       calc_buffsize((plane_size * expanded_bpp + num_comps * 4) * I, 1);
  729.   }
  730.  
  731.   databuff_size = plane_size * storage_bpp;
  732.  
  733.   storage_size_words = ((plane_size + plane_size) * num_comps +
  734.             databuff_size + errbuff_size + outbuff_size) / W;
  735.  
  736.   storage = (ulong *) gs_malloc(storage_size_words, W, "hp_colour_print_page");
  737.  
  738.   /*
  739.    * The principal data pointers are stored as pairs of values, with
  740.    * the selection being made by the 'scan' variable. The function of the
  741.    * scan variable is overloaded, as it controls both the alternating
  742.    * raster scan direction used in the Floyd-Steinberg dithering and also
  743.    * the buffer alternation required for line-difference compression.
  744.    *
  745.    * Thus, the number of pointers required is as follows:
  746.    * 
  747.    *   errors:      2  (scan direction only)
  748.    *   data:        4  (scan direction and alternating buffers)
  749.    *   plane_data:  4  (scan direction and alternating buffers)
  750.    */
  751.  
  752.   if (storage == 0)        /* can't allocate working area */
  753.     return_error(gs_error_VMerror);
  754.   else {
  755.     int i;
  756.     byte *p = out_data = out_row = (byte *)storage;    
  757.     data[0] = data[1] = data[2] = p;
  758.     data[3] = p + databuff_size;
  759.     out_row_alt = out_row + plane_size * 2;
  760.     if (bits_per_pixel > 1) {
  761.       p += databuff_size;
  762.     }
  763.     if (bits_per_pixel > 4) {
  764.       errors[0] = (int *)p + num_comps * 2;
  765.       errors[1] = errors[0] + databuff_size;
  766.       p += errbuff_size;
  767.     }
  768.     for (i = 0; i < num_comps; i++) {
  769.       plane_data[0][i] = plane_data[2][i] = p;
  770.       p += plane_size;
  771.     }
  772.     for (i = 0; i < num_comps; i++) {
  773.       plane_data[1][i] = p;
  774.       plane_data[3][i] = p + plane_size;
  775.       p += plane_size;
  776.     }
  777.     if (bits_per_pixel == 1) {
  778.       out_data = out_row = p;      /* size is outbuff_size * 4 */
  779.       out_row_alt = out_row + plane_size * 2;
  780.       data[1] += databuff_size;   /* coincides with plane_data pointers */
  781.       data[3] += databuff_size;
  782.     }
  783.   }
  784.   
  785.   /* Clear temp storage */
  786.   memset(storage, 0, storage_size_words * W);
  787.   
  788.   /* Initialize printer. */
  789.   fputs("\033E", prn_stream);                       /* Reset printer */
  790.   fputs("\033*rbC", prn_stream);                   /* End raster graphics */
  791.   fprintf(prn_stream, "\033*t%dR", (int)x_dpi);       /* Set resolution */
  792.  
  793. #define DOFFSET (pdev->t_margin - DESKJET_PRINT_LIMIT)  /* Print position */
  794. #define POFFSET (pdev->t_margin - PAINTJET_PRINT_LIMIT)
  795.   switch (ptype) {
  796.   case DJ500C:
  797.   case DJ550C:
  798.     /* Page size, orientation, top margin & perforation skip */
  799.     fprintf(prn_stream, "\033&l%daolE", paper_size);
  800.     /* Set depletion and shingling levels */
  801.     fprintf(prn_stream, "\033*o%dd%dQ", cdj->depletion, cdj->shingling);
  802.     /* Move to top left of printed area */
  803.     fprintf(prn_stream, "\033*p%dY", (int)(300 * DOFFSET));
  804.     /* Set number of planes ((-)1 is mono, (-)3 is (cmy)rgb, -4 is cmyk),
  805.      * and raster width, then start raster graphics */
  806.     fprintf(prn_stream, "\033*r%ds-%du0A", raster_width, num_comps);
  807.     /* Select data compression */
  808.     compression = 9;
  809.     break;
  810.   case PJXL300:
  811.     /* Page size, orientation, top margin & perforation skip */
  812.     fprintf(prn_stream, "\033&l%daolE", paper_size);
  813.     /* Set no-negative-motion mode, for faster (unbuffered) printing */
  814.     fprintf(prn_stream, "\033&a1N");
  815.     /* Set print quality */
  816.     fprintf(prn_stream, "\033*o%dQ", pjxl->printqual);
  817.     /* Move to top left of printed area */
  818.     fprintf(prn_stream, "\033*p%dY", (int)(300 * POFFSET));
  819.     /* Configure colour setup */
  820.     if (pjxl->rendertype > 0) {
  821.       /* Set render type */
  822.       fprintf(prn_stream, "\033*t%dJ", pjxl->rendertype);
  823.       /* Configure image data */
  824.       fputs(cid_string, prn_stream);
  825.       /* Set raster width, then start raster graphics */
  826.       fprintf(prn_stream, "\033*r%ds1A", raster_width);
  827.     } else {
  828.       /* Set number of planes (1 is mono, 3 is rgb),
  829.        * and raster width, then start raster graphics */
  830.       fprintf(prn_stream, "\033*r%ds-%du0A", raster_width, num_comps);
  831.     }
  832.     /* No combined escapes for raster transfers */
  833.     combined_escapes = 0;
  834.     break;
  835.   case PJXL180:
  836.     /* Page size, orientation, top margin & perforation skip */
  837.     fprintf(prn_stream, "\033&l%daolE", paper_size);
  838.     /* Set print quality */
  839.     fprintf(prn_stream, "\033*o%dQ", pjxl->printqual);
  840.     /* Move to top left of printed area */
  841.     fprintf(prn_stream, "\033*p%dY", (int)(180 * POFFSET));
  842.     /* Configure colour setup */
  843.     if (pjxl->rendertype > 0) {
  844.       /* Set render type */
  845.       fprintf(prn_stream, "\033*t%dJ", pjxl->rendertype);
  846.       /* Configure image data */
  847.       fputs(cid_string, prn_stream);
  848.       /* Set raster width, then start raster graphics */
  849.       fprintf(prn_stream, "\033*r%ds1A", raster_width);
  850.     } else {
  851.       /* Set number of planes (1 is mono, 3 is rgb),
  852.        * and raster width, then start raster graphics */
  853.       fprintf(prn_stream, "\033*r%ds%du0A", raster_width, num_comps);
  854.     }
  855.     break;
  856.   case PJ180:
  857.   case DECLJ250:
  858.     /* Disable perforation skip */
  859.     fprintf(prn_stream, "\033&lL");
  860.     /* Move to top left of printed area */
  861.     fprintf(prn_stream, "\033&a%dV", (int)(720 * POFFSET));
  862.     /* Set number of planes (1 is mono, 3 is rgb),
  863.      * and raster width, then start raster graphics */
  864.     fprintf(prn_stream, "\033*r%ds%du0A", raster_width, num_comps);
  865.     if (ptype == DECLJ250) {
  866.       /* No combined escapes for raster transfers */
  867.       combined_escapes = 0;
  868.       /* From here on, we're a standard Paintjet .. */
  869.       ptype = PJ180;
  870.     }
  871.     /* Select data compression */
  872.     compression = 1;
  873.     break;
  874.   }
  875.  
  876.   /* The XL300 PCL interpreter seems to be based on a rather old definition
  877.    * of the language - although it is newer than the deskjets, it includes
  878.    * neither the 'mode 9' compression, nor (even more suprisingly) the ability
  879.    * to use combined escape sequences for the raster transfer commands
  880.    * (where they make by far the biggest impact). Technically, it is a 
  881.    * language extension, but the previous model (the 180dpi Paintjet XL)
  882.    * has this facility, which means that the XL300 is non-backwards-compatible
  883.    * in this respect. Even the original and venerable 180dpi Paintjet has the
  884.    * 'extension'. I have made a guess that the DEC LJ250 does not support
  885.    * it, but have not yet found somebody to test this for me. */
  886.  
  887.   if (combined_escapes) {
  888.     /* From now on, all escape commands start with \033*b, so we
  889.      * combine them. */
  890.     fputs("\033*b", prn_stream);
  891.      /* Set compression if the mode has been defined. */
  892.     if (compression)
  893.       fprintf(prn_stream, "%dm", compression);
  894.   } else
  895.     if (compression)
  896.       fprintf(prn_stream, "\033*b%dM", compression);
  897.  
  898.   /* Send each scan line in turn */
  899.   {
  900.     int lend = pdev->height - (pdev->t_margin + pdev->b_margin) * y_dpi;
  901.     int cErr, mErr, yErr, kErr;
  902.     int this_pass, lnum, i;
  903.     int num_blank_lines = 0;
  904.     int start_rows = (num_comps == 1) ?
  905.       HEAD_ROWS_MONO - 1 : HEAD_ROWS_COLOUR - 1;
  906.     word rmask = ~(word) 0 << ((-pdev->width * storage_bpp) & (W * 8 - 1));
  907.  
  908.     cErr = mErr = yErr = kErr = 0;
  909.  
  910.     if (bits_per_pixel > 4) { /* Randomly seed initial error buffer */
  911.       int *ep = errors[0];
  912.       for (i = 0; i < databuff_size; i++) {
  913.     *ep++ = (rand() % (MAXVALUE / 2))  - MAXVALUE / 4;
  914.       }
  915.     }
  916.  
  917.     /* Inhibit blank line printing for RGB-only printers, 
  918.      * since in this case 'blank' means black! */
  919.     if (ptype == PJ180 || ptype == PJXL180) start_rows = -1;
  920.  
  921.     this_pass = start_rows;
  922.     for (lnum = 0; lnum < lend; lnum++) {
  923.       word *data_words = (word *)data[scan];
  924.       register word *end_data = data_words + line_size_words;
  925.  
  926.       gdev_prn_copy_scan_lines(pdev, lnum, data[scan], line_size);
  927.  
  928.       /* Mask off 1-bits beyond the line width. */
  929.       end_data[-1] &= rmask;
  930.  
  931.       /* Remove trailing 0s. */
  932.       while (end_data > data_words && end_data[-1] == 0)
  933.     end_data--;
  934.       if (end_data == data_words) {    /* Blank line */
  935.     num_blank_lines++;
  936.     continue;
  937.       }
  938.       /* Skip blank lines if any */
  939.       if (num_blank_lines > 0) {
  940.     if (num_blank_lines < this_pass) {
  941.       /* Moving down from current position
  942.        * causes head motion on the DeskJets, so
  943.        * if the number of lines is within the
  944.        * current pass of the print head, we're
  945.        * better off printing blanks. */
  946.       this_pass -= num_blank_lines;
  947.       if (combined_escapes) {
  948.         fputc('y', prn_stream);   /* Clear current and seed rows */
  949.         for (; num_blank_lines; num_blank_lines--)
  950.           fputc('w', prn_stream);
  951.       } else {
  952.         fputs("\033*bY", prn_stream);   /* Clear current and seed rows */
  953.         for (; num_blank_lines; num_blank_lines--)
  954.           fputs("\033*bW", prn_stream);
  955.       }
  956.     } else {
  957.       if (combined_escapes)
  958.         fprintf(prn_stream, "%dy", num_blank_lines);
  959.       else
  960.         fprintf(prn_stream, "\033*b%dY", num_blank_lines);
  961.     }
  962.     memset(plane_data[1 - scan][0], 0, plane_size * num_comps);
  963.     num_blank_lines = 0;
  964.     this_pass = start_rows;
  965.       }
  966.       {            /* Printing non-blank lines */
  967.     register byte *kP = plane_data[scan + 2][3];
  968.     register byte *cP = plane_data[scan + 2][2];
  969.     register byte *mP = plane_data[scan + 2][1];
  970.     register byte *yP = plane_data[scan + 2][0];
  971.     register byte *dp = data[scan + 2];
  972.     register int *ep = errors[scan];
  973.     int zero_row_count;
  974.     int i, j;
  975.     byte *odp;
  976.  
  977.     if (this_pass)
  978.       this_pass--;
  979.     else
  980.       this_pass = start_rows;
  981.  
  982.     if (expanded_bpp > bits_per_pixel)   /* Expand line if required */
  983.       expand_line(data_words, line_size, bits_per_pixel, expanded_bpp);
  984.  
  985.     /* In colour modes, we have some bit-shuffling to do before
  986.      * we can print the data; in FS mode we also have the
  987.      * dithering to take care of. */
  988.     switch (expanded_bpp) {    /* Can be 1, 3, 8, 24 or 32 */
  989.     case 3:
  990.       /* Transpose the data to get pixel planes. */
  991.       for (i = 0, odp = plane_data[scan][0]; i < databuff_size;
  992.            i += 8, odp++) {    /* The following is for 16-bit
  993.                  * machines */
  994. #define spread3(c)\
  995.     { 0, c, c*0x100, c*0x101, c*0x10000L, c*0x10001L, c*0x10100L, c*0x10101L }
  996.         static ulong spr40[8] = spread3(0x40);
  997.         static ulong spr08[8] = spread3(8);
  998.         static ulong spr02[8] = spread3(2);
  999.         register byte *dp = data[scan] + i;
  1000.         register ulong pword =
  1001.         (spr40[dp[0]] << 1) +
  1002.         (spr40[dp[1]]) +
  1003.         (spr40[dp[2]] >> 1) +
  1004.         (spr08[dp[3]] << 1) +
  1005.         (spr08[dp[4]]) +
  1006.         (spr08[dp[5]] >> 1) +
  1007.         (spr02[dp[6]]) +
  1008.         (spr02[dp[7]] >> 1);
  1009.         odp[0] = (byte) (pword >> 16);
  1010.         odp[plane_size] = (byte) (pword >> 8);
  1011.         odp[plane_size * 2] = (byte) (pword);
  1012.       }
  1013.       break;
  1014.  
  1015.     case 8:
  1016.       FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
  1017.           cP, mP, yP, kP, 1);
  1018.       break;
  1019.     case 24:
  1020.       FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
  1021.           cP, mP, yP, kP, 3);
  1022.       break;
  1023.     case 32:
  1024.       FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
  1025.           cP, mP, yP, kP, 4);
  1026.       break;
  1027.  
  1028.     } /* switch(expanded_bpp) */
  1029.  
  1030.     /* Make sure all black is in the k plane */
  1031.         if (num_comps == 4 ) {
  1032.       register word *kp = (word *)plane_data[scan][3];
  1033.       register word *cp = (word *)plane_data[scan][2];
  1034.       register word *mp = (word *)plane_data[scan][1];
  1035.       register word *yp = (word *)plane_data[scan][0];
  1036.       if (bits_per_pixel > 4) {  /* This has been done as 4 planes */
  1037.         for (i = 0; i < plane_size / W; i++) {
  1038.           word bits = ~*kp++;
  1039.           *cp++ &= bits;
  1040.           *mp++ &= bits;
  1041.           *yp++ &= bits;
  1042.         }
  1043.       } else {  /* This has really been done as 3 planes */
  1044.         for (i = 0; i < plane_size / W; i++) {
  1045.           word bits = *cp & *mp & *yp;
  1046.           *kp++ = bits;
  1047.           bits = ~bits;
  1048.           *cp++ &= bits;
  1049.           *mp++ &= bits;
  1050.           *yp++ &= bits;
  1051.         }
  1052.       }
  1053.         }
  1054.  
  1055.     /* Transfer raster graphics
  1056.      * in the order (K), C, M, Y. */
  1057.     for (zero_row_count = 0, i = num_comps - 1; i >= 0; i--) {
  1058.       int output_plane = 1;
  1059.       int out_count;
  1060.       
  1061.       switch (ptype) {
  1062.       case DJ500C:    /* Always compress using mode 9 */
  1063.       case DJ550C:
  1064.         out_count = gdev_pcl_mode9compress(plane_size,
  1065.                            plane_data[scan][i],
  1066.                            plane_data[1 - scan][i],
  1067.                            out_data);
  1068.  
  1069.         /* This optimisation allows early termination of the
  1070.          * row, but this doesn't work correctly in an alternating
  1071.          * mode 2 / mode 3 regime, so we only use it with mode 9
  1072.          * compression */
  1073.            if (out_count == 0)
  1074.              { output_plane = 0;      /* No further output for this plane */
  1075.                if (i == 0)
  1076.                  fputc('w', prn_stream);
  1077.                else
  1078.                  zero_row_count++;
  1079.              }
  1080.            else
  1081.              { for (; zero_row_count; zero_row_count--)
  1082.                  fputc('v', prn_stream);
  1083.              }
  1084.         break;
  1085.       case PJ180:
  1086.         /* NB: The mode 1 routine complements the data -> rgb */
  1087.         out_count = gdev_pcl_mode1compress((const byte *)
  1088.                            plane_data[scan][i],
  1089.                            (const byte *)
  1090.                            plane_data[scan][i] + plane_size - 1,
  1091.                            out_data);
  1092.         break;
  1093.       case PJXL180:    /* Need to invert data as CMY not supported */
  1094.         if (num_comps > 1)
  1095.           { word *wp = (word *)plane_data[scan][i];
  1096.         for (j = 0; j < plane_size / W; j++, wp++)
  1097.           *wp = ~*wp;
  1098.           }
  1099.         /* fall through .. */
  1100.       case PJXL300:     /* Compression modes 2 and 3 are both 
  1101.                  * available.  Try both and see which one
  1102.                  * produces the least output data. */
  1103.         { const char *plane = (char *)plane_data[scan][i];
  1104.           char *prev_plane = (char *)plane_data[1 - scan][i];
  1105.           const word *row = (word *)plane;
  1106.           const word *end_row = row + plane_size/W;
  1107.           int count2 = gdev_pcl_mode2compress(row, end_row, out_row_alt);
  1108.           int count3 = gdev_pcl_mode3compress(plane_size, plane, prev_plane, out_row);
  1109.           int penalty = combined_escapes ? strlen("#m") : strlen("\033*b#M");
  1110.           int penalty2 = (compression == 2 ? 0 : penalty);
  1111.           int penalty3 = (compression == 3 ? 0 : penalty);
  1112.           
  1113.           if (count3 + penalty3 < count2 + penalty2)
  1114.         { if ( compression != 3 ) {
  1115.             if (combined_escapes)
  1116.               fputs("3m", prn_stream);
  1117.             else
  1118.               fputs("\033*b3M", prn_stream);
  1119.             compression = 3;
  1120.           }
  1121.           out_data = out_row;
  1122.           out_count = count3;
  1123.         }
  1124.           else
  1125.         { if ( compression != 2 ) {
  1126.             if (combined_escapes)
  1127.               fputs("2m", prn_stream);
  1128.             else
  1129.               fputs("\033*b2M", prn_stream);
  1130.             compression = 2;
  1131.           }
  1132.           out_data = out_row_alt;
  1133.           out_count = count2;
  1134.         }
  1135.         }
  1136.         break;
  1137.       }
  1138.       if (output_plane) {
  1139.         if (combined_escapes)
  1140.           fprintf(prn_stream, "%d%c", out_count, "wvvv"[i]);
  1141.         else
  1142.           fprintf(prn_stream, "\033*b%d%c", out_count, "WVVV"[i]);
  1143.         fwrite(out_data, sizeof(byte), out_count, prn_stream);
  1144.       }
  1145.       
  1146.     } /* Transfer Raster Graphics ... */
  1147.     scan = 1 - scan;          /* toggle scan direction */
  1148.       }      /* Printing non-blank lines */
  1149.     }     /* for lnum ... */
  1150.   }       /* send each scan line in turn */
  1151.  
  1152.   if (combined_escapes)
  1153.     fputs("0M", prn_stream);
  1154.  
  1155.   /* end raster graphics & reset printer */
  1156.   fputs("\033*rbC\033E", prn_stream);
  1157.  
  1158.   /* eject page */
  1159.   if (ptype == PJ180)
  1160.     fputc('\f', prn_stream);
  1161.   else
  1162.     fputs("\033&l0H", prn_stream);
  1163.  
  1164.   /* free temporary storage */
  1165.   gs_free((char *) storage, storage_size_words, W, "hp_colour_print_page");
  1166.  
  1167.   return 0;
  1168. }
  1169.  
  1170. /*
  1171.  * Mode 9 2D compression for the HP DeskJet 5xxC. This mode can give
  1172.  * very good compression ratios, especially if there are areas of flat
  1173.  * colour (or blank areas), and so is 'highly recommended' for colour
  1174.  * printing in particular because of the very large amounts of data which
  1175.  * can be generated
  1176.  */
  1177. private int
  1178. gdev_pcl_mode9compress(int bytecount, const byte * current, const byte * previous, byte * compressed)
  1179. {
  1180.   register const byte *cur = current;
  1181.   register const byte *prev = previous;
  1182.   register byte *out = compressed;
  1183.   const byte *end = current + bytecount;
  1184.  
  1185.   while (cur < end) {        /* Detect a run of unchanged bytes. */
  1186.     const byte *run = cur;
  1187.     register const byte *diff;
  1188.     int offset;
  1189.     while (cur < end && *cur == *prev) {
  1190.       cur++, prev++;
  1191.     }
  1192.     if (cur == end)
  1193.       break;            /* rest of row is unchanged */
  1194.     /* Detect a run of changed bytes. */
  1195.     /* We know that *cur != *prev. */
  1196.     diff = cur;
  1197.     do {
  1198.       prev++;
  1199.       cur++;
  1200.     }
  1201.     while (cur < end && *cur != *prev);
  1202.     /* Now [run..diff) are unchanged, and */
  1203.     /* [diff..cur) are changed. */
  1204.     offset = diff - run;
  1205.     {
  1206.       const byte *stop_test = cur - 4;
  1207.       int dissimilar, similar;
  1208.  
  1209.       while (diff < cur) {
  1210.     const byte *compr = diff;
  1211.     const byte *next;    /* end of run */
  1212.     byte value;
  1213.     while (diff <= stop_test &&
  1214.            ((value = *diff) != diff[1] ||
  1215.         value != diff[2] ||
  1216.         value != diff[3]))
  1217.       diff++;
  1218.  
  1219.     /* Find out how long the run is */
  1220.     if (diff > stop_test)    /* no run */
  1221.       next = diff = cur;
  1222.     else {
  1223.       next = diff + 4;
  1224.       while (next < cur && *next == value)
  1225.         next++;
  1226.     }
  1227.  
  1228. #define MAXOFFSETU 15
  1229. #define MAXCOUNTU 7
  1230.     /* output 'dissimilar' bytes, uncompressed */
  1231.     if ((dissimilar = diff - compr)) {
  1232.       int temp, i;
  1233.  
  1234.       if ((temp = --dissimilar) > MAXCOUNTU)
  1235.         temp = MAXCOUNTU;
  1236.       if (offset < MAXOFFSETU)
  1237.         *out++ = (offset << 3) | (byte) temp;
  1238.       else {
  1239.         *out++ = (MAXOFFSETU << 3) | (byte) temp;
  1240.         offset -= MAXOFFSETU;
  1241.         while (offset >= 255) {
  1242.           *out++ = 255;
  1243.           offset -= 255;
  1244.         }
  1245.         *out++ = offset;
  1246.       }
  1247.       if (temp == MAXCOUNTU) {
  1248.         temp = dissimilar - MAXCOUNTU;
  1249.         while (temp >= 255) {
  1250.           *out++ = 255;
  1251.           temp -= 255;
  1252.         }
  1253.         *out++ = (byte) temp;
  1254.       }
  1255.       for (i = 0; i <= dissimilar; i++)
  1256.         *out++ = *compr++;
  1257.       offset = 0;
  1258.     }            /* end uncompressed */
  1259. #define MAXOFFSETC 3
  1260. #define MAXCOUNTC 31
  1261.     /* output 'similar' bytes, run-length encoded */
  1262.     if ((similar = next - diff)) {
  1263.       int temp;
  1264.  
  1265.       if ((temp = (similar -= 2)) > MAXCOUNTC)
  1266.         temp = MAXCOUNTC;
  1267.       if (offset < MAXOFFSETC)
  1268.         *out++ = 0x80 | (offset << 5) | (byte) temp;
  1269.       else {
  1270.         *out++ = 0x80 | (MAXOFFSETC << 5) | (byte) temp;
  1271.         offset -= MAXOFFSETC;
  1272.         while (offset >= 255) {
  1273.           *out++ = 255;
  1274.           offset -= 255;
  1275.         }
  1276.         *out++ = offset;
  1277.       }
  1278.       if (temp == MAXCOUNTC) {
  1279.         temp = similar - MAXCOUNTC;
  1280.         while (temp >= 255) {
  1281.           *out++ = 255;
  1282.           temp -= 255;
  1283.         }
  1284.         *out++ = (byte) temp;
  1285.       }
  1286.       *out++ = value;
  1287.       offset = 0;
  1288.     }            /* end compressed */
  1289.     diff = next;
  1290.       }
  1291.     }
  1292.   }
  1293.   return out - compressed;
  1294. }
  1295.  
  1296. /*
  1297.  * Row compression for the H-P PaintJet.
  1298.  * Compresses data from row up to end_row, storing the result
  1299.  * starting at compressed.  Returns the number of bytes stored.
  1300.  * The compressed format consists of a byte N followed by a
  1301.  * data byte that is to be repeated N+1 times.
  1302.  * In the worst case, the `compressed' representation is
  1303.  * twice as large as the input.
  1304.  * We complement the bytes at the same time, because
  1305.  * we accumulated the image in complemented form.
  1306.  */
  1307. private int
  1308. gdev_pcl_mode1compress(const byte *row, const byte *end_row, byte *compressed)
  1309. {       register const byte *in = row;
  1310.         register byte *out = compressed;
  1311.         while ( in < end_row )
  1312.            {    byte test = *in++;
  1313.                 const byte *run = in;
  1314.                 while ( in < end_row && *in == test ) in++;
  1315.                 /* Note that in - run + 1 is the repetition count. */
  1316.                 while ( in - run > 255 )
  1317.                    {    *out++ = 255;
  1318.                         *out++ = ~test;
  1319.                         run += 256;
  1320.                    }
  1321.                 *out++ = in - run;
  1322.                 *out++ = ~test;
  1323.            }
  1324.         return out - compressed;
  1325. }
  1326.  
  1327. /*
  1328.  * Map a r-g-b color to a color index.
  1329.  * We complement the colours, since we're using cmy anyway, and
  1330.  * because the buffering routines expect white to be zero.
  1331.  * Includes colour balancing, following HP recommendations, to try
  1332.  * and correct the greenish cast resulting from an equal mix of the
  1333.  * c, m, y, inks by reducing the cyan component to give a truer black.
  1334.  */
  1335. private gx_color_index
  1336. gdev_pcl_map_rgb_color(gx_device *pdev, gx_color_value r,
  1337.                  gx_color_value g, gx_color_value b)
  1338. {
  1339.   if (gx_color_value_to_byte(r & g & b) == 0xff)
  1340.     return (gx_color_index)0;         /* white */
  1341.   else {
  1342.     unsigned short r2,g2,b2,rr,gg,bb;
  1343.     float r3,g3,b3;
  1344.     int correction = hp_device->correction;
  1345.     gx_color_value c ;
  1346.     gx_color_value m ;
  1347.     gx_color_value y ;
  1348.  
  1349.     if (CCOR[0][0][0][0]!=-1.0) {
  1350.         /* color correction added by Steve Ludtke 1994 */
  1351.         r2=gx_color_value_to_byte(r);
  1352.         g2=gx_color_value_to_byte(g);
  1353.         b2=gx_color_value_to_byte(b);
  1354.         r3=(float)(r2&31)/32.0;
  1355.         g3=(float)(g2&31)/32.0;
  1356.         b3=(float)(b2&31)/32.0;
  1357.         /*fprintf(stdout,"%d %d %d ",r2,g2,b2);*/
  1358.         r2=r2>>5;
  1359.         g2=g2>>5;
  1360.         b2=b2>>5;
  1361.         /*fprintf(stdout,"%x %x %x %f %f %f ->",r2,g2,b2,r3,g3,b3);*/
  1362.         if (r3==0&&b3==0&g3==0) {
  1363.             rr=CCOR[r2][g2][b2][0];
  1364.             gg=CCOR[r2][g2][b2][1];
  1365.             bb=CCOR[r2][g2][b2][2];
  1366.         }
  1367.         else {
  1368.         /*    fprintf(stdout,"= %f %f %f =", CCOR[r2][g2][b2][0],CCOR[r2][g2][b2][1],CCOR[r2][g2][b2][2]);*/
  1369.             rr=CCOR[r2][g2][b2][0]*(1.0-r3)*(1.0-g3)*(1.0-b3)+
  1370.                 CCOR[r2][g2][b2+1][0]*(1.0-r3)*(1.0-g3)*b3+
  1371.                 CCOR[r2][g2+1][b2][0]*(1.0-r3)*g3*(1.0-b3)+
  1372.                 CCOR[r2][g2+1][b2+1][0]*(1.0-r3)*g3*b3+
  1373.                 CCOR[r2+1][g2][b2][0]*r3*(1.0-g3)*(1.0-b3)+
  1374.                 CCOR[r2+1][g2][b2+1][0]*r3*(1.0-g3)*b3+
  1375.                 CCOR[r2+1][g2+1][b2][0]*r3*g3*(1.0-b3)+
  1376.                 CCOR[r2+1][g2+1][b2+1][0]*r3*g3*b3;
  1377.             gg=CCOR[r2][g2][b2][1]*(1.0-r3)*(1.0-g3)*(1.0-b3)+
  1378.                 CCOR[r2][g2][b2+1][1]*(1.0-r3)*(1.0-g3)*b3+
  1379.                 CCOR[r2][g2+1][b2][1]*(1.0-r3)*g3*(1.0-b3)+
  1380.                 CCOR[r2][g2+1][b2+1][1]*(1.0-r3)*g3*b3+
  1381.                 CCOR[r2+1][g2][b2][1]*r3*(1.0-g3)*(1.0-b3)+
  1382.                 CCOR[r2+1][g2][b2+1][1]*r3*(1.0-g3)*b3+
  1383.                 CCOR[r2+1][g2+1][b2][1]*r3*g3*(1.0-b3)+
  1384.                 CCOR[r2+1][g2+1][b2+1][1]*r3*g3*b3;
  1385.             bb=CCOR[r2][g2][b2][2]*(1.0-r3)*(1.0-g3)*(1.0-b3)+
  1386.                 CCOR[r2][g2][b2+1][2]*(1.0-r3)*(1.0-g3)*b3+
  1387.                 CCOR[r2][g2+1][b2][2]*(1.0-r3)*g3*(1.0-b3)+
  1388.                 CCOR[r2][g2+1][b2+1][2]*(1.0-r3)*g3*b3+
  1389.                 CCOR[r2+1][g2][b2][2]*r3*(1.0-g3)*(1.0-b3)+
  1390.                 CCOR[r2+1][g2][b2+1][2]*r3*(1.0-g3)*b3+
  1391.                 CCOR[r2+1][g2+1][b2][2]*r3*g3*(1.0-b3)+
  1392.                 CCOR[r2+1][g2+1][b2+1][2]*r3*g3*b3;
  1393.         }
  1394.         if (rr>255) rr=0; else rr=255-rr;
  1395.         if (gg>255) gg=0; else gg=255-gg;
  1396.         if (bb>255) bb=0; else bb=255-bb;
  1397.         /*fprintf(stdout," %d %d %d\n",rr,gg,bb);*/
  1398.         c=gx_color_value_from_byte(rr);
  1399.         m=gx_color_value_from_byte(gg);
  1400.         y=gx_color_value_from_byte(bb);
  1401.     }
  1402.     else {
  1403.         c = gx_max_color_value - r;
  1404.         m = gx_max_color_value - g;
  1405.         y = gx_max_color_value - b;
  1406.     }
  1407.  
  1408.     /* Colour correction for better blacks when using the colour ink
  1409.      * cartridge (on the DeskJet 500C only). We reduce the cyan component
  1410.      * by some fraction (eg. 4/5) to correct the slightly greenish cast
  1411.      * resulting from an equal mix of the three inks */
  1412.     if (correction) {
  1413.       ulong maxval, minval, range;
  1414.       
  1415.       maxval = c >= m ? (c >= y ? c : y) : (m >= y ? m : y);
  1416.       if (maxval > 0) {
  1417.     minval = c <= m ? (c <= y ? c : y) : (m <= y? m : y);
  1418.     range = maxval - minval;
  1419.     
  1420. #define shift (gx_color_value_bits - 12)
  1421.     c = ((c >> shift) * (range + (maxval * correction))) /
  1422.       ((maxval * (correction + 1)) >> shift);
  1423.       }
  1424.     }
  1425.     
  1426.     switch (pdev->color_info.depth) {
  1427.     case 1:
  1428.       return ((c | m | y) > gx_max_color_value / 2 ?
  1429.           (gx_color_index)1 : (gx_color_index)0);
  1430.     case 8:
  1431.       if (pdev->color_info.num_components >= 3)
  1432. #define gx_color_value_to_1bit(cv) ((cv) >> (gx_color_value_bits - 1))
  1433.     return (gx_color_value_to_1bit(c) +
  1434.         (gx_color_value_to_1bit(m) << 1) +
  1435.         (gx_color_value_to_1bit(y) << 2));
  1436.       else
  1437. #define red_weight 306
  1438. #define green_weight 601
  1439. #define blue_weight 117
  1440.     return ((((ulong)c * red_weight +
  1441.           (ulong)m * green_weight +
  1442.           (ulong)y * blue_weight)
  1443.          >> (gx_color_value_bits + 2)));
  1444.     case 16:
  1445. #define gx_color_value_to_5bits(cv) ((cv) >> (gx_color_value_bits - 5))
  1446. #define gx_color_value_to_6bits(cv) ((cv) >> (gx_color_value_bits - 6))
  1447.       return (gx_color_value_to_5bits(y) +
  1448.           (gx_color_value_to_6bits(m) << 5) +
  1449.           (gx_color_value_to_5bits(c) << 11));
  1450.     case 24:
  1451.       return (gx_color_value_to_byte(y) +
  1452.           (gx_color_value_to_byte(m) << 8) +
  1453.           ((ulong)gx_color_value_to_byte(c) << 16));
  1454.     case 32:
  1455.       { gx_color_value k = c <= m ? (c <= y ? c : y) : (m <= y ? m : y);
  1456.     return (gx_color_value_to_byte(y - k) +
  1457.         (gx_color_value_to_byte(m - k) << 8) +
  1458.         ((ulong)gx_color_value_to_byte(c - k) << 16) +
  1459.         ((ulong)gx_color_value_to_byte(k) << 24));
  1460.       }
  1461.     }
  1462.   }
  1463.   return (gx_color_index)0;   /* This never happens */
  1464. }
  1465.     
  1466. /* Map a color index to a r-g-b color. */
  1467. private int
  1468. gdev_pcl_map_color_rgb(gx_device *pdev, gx_color_index color,
  1469.                 gx_color_value prgb[3])
  1470. {
  1471.   /* For the moment, we simply ignore any black correction */
  1472.   switch (pdev->color_info.depth) {
  1473.   case 1:
  1474.     prgb[0] = prgb[1] = prgb[2] = -((gx_color_value)color ^ 1);
  1475.     break;
  1476.   case 8:
  1477.       if (pdev->color_info.num_components >= 3)
  1478.     { gx_color_value c = (gx_color_value)color ^ 7;
  1479.       prgb[0] = -(c & 1);
  1480.       prgb[1] = -((c >> 1) & 1);
  1481.       prgb[2] = -(c >> 2);
  1482.     }
  1483.       else
  1484.     { gx_color_value value = (gx_color_value)color ^ 0xff;
  1485.       prgb[0] = prgb[1] = prgb[2] = (value << 8) + value;
  1486.     }
  1487.     break;
  1488.   case 16:
  1489.     { gx_color_value c = (gx_color_value)color ^ 0xffff;
  1490.       ushort value = c >> 11;
  1491.       prgb[0] = ((value << 11) + (value << 6) + (value << 1) +
  1492.          (value >> 4)) >> (16 - gx_color_value_bits);
  1493.       value = (c >> 6) & 0x3f;
  1494.       prgb[1] = ((value << 10) + (value << 4) + (value >> 2))
  1495.     >> (16 - gx_color_value_bits);
  1496.       value = c & 0x1f;
  1497.       prgb[2] = ((value << 11) + (value << 6) + (value << 1) +
  1498.          (value >> 4)) >> (16 - gx_color_value_bits);
  1499.     }
  1500.     break;
  1501.   case 24:
  1502.     { gx_color_value c = (gx_color_value)color ^ 0xffffff;
  1503.       prgb[0] = gx_color_value_from_byte(c >> 16);
  1504.       prgb[1] = gx_color_value_from_byte((c >> 8) & 0xff);
  1505.       prgb[2] = gx_color_value_from_byte(c & 0xff);
  1506.     }
  1507.     break;
  1508.   case 32:
  1509. #define  gx_maxcol gx_color_value_from_byte(gx_color_value_to_byte(gx_max_color_value))
  1510.     { gx_color_value w = gx_maxcol - gx_color_value_from_byte(color >> 24);
  1511.       prgb[0] = w - gx_color_value_from_byte((color >> 16) & 0xff);
  1512.       prgb[1] = w - gx_color_value_from_byte((color >> 8) & 0xff);
  1513.       prgb[2] = w - gx_color_value_from_byte(color & 0xff);
  1514.     }
  1515.     break;
  1516.   }
  1517.   return 0;
  1518. }
  1519.  
  1520. /*
  1521.  * Convert and expand scanlines:
  1522.  *
  1523.  *       (a)    16 -> 24 bit   (1-stage)
  1524.  *       (b)    16 -> 32 bit   (2-stage)
  1525.  *   or  (c)    24 -> 32 bit   (1-stage)
  1526.  */
  1527. private void
  1528. expand_line(word *line, int linesize, int bpp, int ebpp)
  1529. {
  1530.   int endline = linesize;
  1531.   byte *start = (byte *)line;
  1532.   register byte *in, *out;
  1533.  
  1534.   if (bpp == 16)              /* 16 to 24 (cmy) if required */
  1535.     { register byte b0, b1;
  1536.       endline = ((endline + 1) / 2);
  1537.       in = start + endline * 2;
  1538.       out = start + (endline *= 3);
  1539.       
  1540.       while (in > start)
  1541.     { b0 = *--in;
  1542.       b1 = *--in;
  1543.       *--out = (b0 << 3) + ((b0 >> 2) & 0x7);
  1544.       *--out = (b1 << 5) + ((b0 >> 3)  & 0x1c) + ((b1 >> 1) & 0x3);
  1545.       *--out = (b1 & 0xf8) + (b1 >> 5);
  1546.     }
  1547.     }
  1548.  
  1549.   if (ebpp == 32)             /* 24 (cmy) to 32 (cmyk) if required */
  1550.     { register byte c, m, y, k;
  1551.       endline = ((endline + 2) / 3);
  1552.       in = start + endline * 3;
  1553.       out = start + endline * 4;
  1554.  
  1555.       while (in > start)
  1556.     { y = *--in;
  1557.       m = *--in;
  1558.       c = *--in;
  1559.       k = c < m ? (c < y ? c : y) : (m < y ? m : y);
  1560.       *--out = y - k;
  1561.       *--out = m - k;
  1562.       *--out = c - k;
  1563.       *--out = k;
  1564.     }
  1565.     }
  1566. }
  1567.  
  1568. private int
  1569. put_prop_int(gs_prop_item *pi, int *property, int minval, int maxval, int code)
  1570. {
  1571.   if ( pi == 0 )
  1572.     return (code);
  1573.   
  1574.   if ( pi->value.i < minval || pi->value.i > maxval )
  1575.     { pi->status = pv_rangecheck;
  1576.       return (gs_error_rangecheck);
  1577.     }
  1578.   else
  1579.     { *property = pi->value.i;
  1580.       return (code ? code : 1);
  1581.     }
  1582. }    
  1583.  
  1584. private void
  1585. set_bpp(gx_device *pdev, int bits_per_pixel)
  1586. { gx_device_color_info *ci = &pdev->color_info;
  1587.   /* Only valid bits-per-pixel are 1, 3, 8, 16, 24, 32 */
  1588.   int bpp = bits_per_pixel < 3 ? 1 : bits_per_pixel < 8 ? 3 : 
  1589.     (bits_per_pixel >> 3) << 3;
  1590.   ci->num_components = ((bpp == 1) || (bpp == 8) ? 1 : 3);
  1591.   ci->depth = ((bpp > 1) && (bpp < 8) ? 8 : bpp);
  1592.   ci->max_gray = (bpp >= 8 ? 255 : 1);
  1593.   ci->max_rgb = (bpp >= 8 ? 255 : bpp > 1 ? 1 : 0);
  1594.   ci->dither_gray = (bpp >= 8 ? 5 : 2);
  1595.   ci->dither_rgb = (bpp >= 8 ? 5 : bpp > 1 ? 2 : 0);
  1596. }
  1597.  
  1598. /* This returns either the number of pixels in a scan line, or the number
  1599.  * of bytes required to store the line, both clipped to the page margins */
  1600. private uint
  1601. gdev_prn_rasterwidth(const gx_device_printer *pdev, int pixelcount)
  1602. {
  1603.   ulong raster_width =
  1604.     pdev->width - pdev->x_pixels_per_inch * (pdev->l_margin + pdev->r_margin);
  1605.   return (pixelcount ?
  1606.           (uint)raster_width :
  1607.           (uint)((raster_width * pdev->color_info.depth + 7) >> 3));
  1608. }
  1609.